<?php
class RelationController extends ManagerController {
	public function RelationController(){
		parent::ManagerController();
	}
	
	public function index(){
		$relation_mod = new RelationModel();
		$relations = $relation_mod->find(array('site_id'=>SITE_ID));
		$tree = new Tree($relations);
		$this->assign('relations', $tree->get_formated_list());
		$this->display();
	}
	
	public function edit(){
		if(IS_POST){
			self::save();
		}
		$relation_mod = new RelationModel();
		if(isset($this->id)){
			//修改
			$relation = $relation_mod->get_by_attrs(array('site_id'=>SITE_ID, '_id'=>$this->id));
			$this->assign('relation', $relation);
			
			//获取当前所有分类
			$relations = $relation_mod->get_list($relation, Lang::get_langs());
			lang_fatch($relations, LANGUAGE);
			$tree = new Tree($relations);
			$this->assign('relations', $tree->get_formated_list());
			$options = $tree->get_options('　');
			$this->assign('options', $options);
			$this->assign('id', $this->id);
		}
		$rid = self::get_val('rid');
		if(!empty($rid)){
			//添加下级分类
			$this->assign('rid', $rid);
		}
		$this->assign('owners', FormsModel::get_form_owner());
		$this->display();
	}
	

	private function save(){
		$code = self::get_post_val('code');
		//检查合法性
		if (!preg_match('/[[a-z|A-Z][a-z|A-Z|0-9|_]+/i', $code)) {
			self::ajax_error('代码不合法！');
		}
		$owner = self::get_post_val('owner', 'user');
		$relation = array('code' => $code, 'site_id'=>SITE_ID);
		foreach ($this->site_info['langs'] as $lang=>$is_default){
			$name = self::get_post_val($lang . '_name');
			if(empty($name) || utf8_strlen($name)>50){
				self::ajax_error('名称[' . Lang::get_lan_name($lang) . ']不正确！');
			}
			$desc = self::get_post_val($lang . '_desc');
			$relation[$lang] = array(
				'name' => $name,
				'desc' => $desc,
			);
		}
		$relation['owner'] = $owner;
		$relation_mod = new RelationModel();
		if(empty($this->id)){
			//新建
			//检查代码是否存在
			if($relation_mod->exists_attrs(array('site_id'=>SITE_ID, 'code'=>$code))){
				self::ajax_error('此代码已经存在！');
			}
			$relation['_id'] = gen_guid();
			$relation_mod->insert($relation);
			self::ajax_info('新增成功！', $relation['_id']);
		}else{
			//更新
			//检查代码是否存在
			if($relation_mod->exists_attrs(array('site_id'=>SITE_ID, 'code'=>$code, '_id'=>array('$ne'=>$this->id)))){
				self::ajax_error('此代码已经存在！');
			}
			$relation_mod->update_by_key($this->id, $relation);
			$relation['_id'] = $this->id;
			self::ajax_info('修改成功！', $this->id);
		}
	}
	
	public function item_edit(){
		if(IS_POST){
			self::item_save();
		}
		$relation_mod = new RelationModel();
		//编辑
		if(isset($_REQUEST['rid'])){
			//修改
			$rid = self::get_val('rid');
			$relation = $relation_mod->get_by_key_site_limit($this->id, $rid);
			$relation[$rid]['_id'] = $rid;
			$this->assign('relation_item', $relation[$rid]);
		}else{
			//更新
			
		}
		//加载当前关联属性
		$relations = $relation_mod->get_list($this->id, Lang::get_langs());
		$relations = lang_fatch($relations, LANGUAGE);
		$tree = new Tree($relations);
		$options = $tree->get_options('　');
		//删除自己
		if(isset($rid)){
			unset($options[$rid]);
		}
		$this->assign('options', $options);
		$this->assign('id', $this->id);
		$this->display();
	}
	
	private function item_save(){
		$rid = self::get_post_val('rid');
		$item = array();
		$parent = self::get_post_val('parent','');
		$relation_mod = new RelationModel();
		if(!empty($parent)){
			//如果上级不为空
			$parent_relation = $relation_mod->get_by_key_site_limit($this->id, $parent);
			if(empty($parent_relation)){
				self::ajax_error('父级不存在！');
			}
			$parent_relation = $parent_relation[$parent];
			$item['parent'] = $parent;
			if(isset($parent_relation['parent_ids'])){
				$item['parent_ids'] = $parent_relation['parent_ids'] . '.' . $parent;
			}else{
				$item['parent_ids'] = $parent;
			}
		}
		foreach ($this->site_info['langs'] as $lang=>$is_default){
			$name = self::get_post_val($lang . '_name');
			$desc = self::get_post_val($lang . '_desc');
			if(empty($name) || utf8_strlen($name)>50){
				self::ajax_error('名称[' . Lang::get_lan_name($lang) . ']不正确！');
			}
			$item[$lang] = array(
				'name' => $name,
				'desc' => $desc,
			);
			if(isset($parent_relation) && isset($parent_relation[$lang]['parent_names'])){
				$item[$lang]['parent_names'] = $parent_relation[$lang]['parent_names'] . '.' . $parent_relation[$lang]['name'];
			}else{
				$item[$lang]['parent_names'] = $parent_relation[$lang]['name'];
			}
		}
		if(empty($rid)){
			//新增
			$rid = self::get_post_val('code');
			if(empty($rid)){
				$rid = gen_guid();
			}else{
				//
				if (!preg_match('/[a-z|A-Z|0-9|_]+/i', $rid)) {
					self::ajax_error('属性值不合法，必须以字母开头，只能使用数字、字母和下划线！');
				}
				$columns = RelationModel::get_base_columns();
				if(in_array($rid, $columns)){
					self::ajax_error($rid . '为保留字，属性值不能为此值！');
				}
			}
			$relation_mod->update_by_key($this->id, array($rid => $item));
			self::ajax_info('添加成功！', array('_id'=>$this->id, 'rid'=>$rid));
		}else{
			//修改
			$relation_mod->update_by_key($this->id, array($rid => $item));
			self::ajax_info('修改成功！', array('_id'=>$this->id, 'rid'=>$rid));
		}
	}
	
	public function del(){
		if(empty($this->id)){
			self::ajax_error('未指定要删除的关联属性！');
		}
		$relation_mod = new RelationModel();
		$relation_mod->delete_by_key_site_limit($this->id);
		self::ajax_info('删除成功！');
	}
	
	public function del_item(){
		$rid = self::get_val('rid');
		if(empty($this->id) || empty($rid)){
			self::ajax_error('未指定要删除的条目！');
		}
		$relation_mod = new RelationModel();
		$relation_mod->delete_column_by_key($this->id, $rid);
		self::ajax_info('删除成功！');
		
	}
}

?>